/*
 * @Descripttion:
 * @version:
 * @Author: mmzhang
 * @Date: 2021-07-27 16:02:34
 * @LastEditTime: 2023-02-17 15:16:31
 */
import table from '@/components/dt/table/render.js'
import nameOpt from '@/biz/option/name.js'
import dateOpt from '@/biz/option/date.js'
import houseOpt from '@/biz/option/house.js'
import Opt from '@/components/dt/info/Opt.js'
import dt from '@dt/dt'
import channelTypeMgr from '@/biz/option/channelType'
import planOpt from '@/biz/option/plan.js'

let _this = null

class Column extends Opt {
  constructor(vm) {
    super()
    _this = vm
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [
      {
        title: '退购单编号',
        field: 'backCode',
        minWidth: 150
      },
      {
        title: '所属子公司',
        field: 'orgName',
        minWidth: 150
      },
      {
        title: '所属案场',
        field: 'housesName',
        minWidth: 150
      },

      {
        title: '退购状态',
        field: 'businessResult',
        minWidth: 130,
        cell: (item) => {
          return {
            text: {
              type: 'dict',
              key: 'backStatus',
              color: item.businessResult == 3 ? '#D9001B' : '#515a6e'
            }
          }
        }
      },
      {
        minWidth: 130,
        field: 'counselorName',
        header: {
          filter: nameOpt.createCounselorPeople({
            key: 'counselorId',
            title: '归属顾问',
            to: (info) => {
              return {
                counselorId: info.userId
              }
            }
          })
        }
      },
      {
        title: '客户姓名',
        field: 'customerName',
        minWidth: 120,
        slots: {
          default: (params, h) => {
            return table.cell.btn(_this, h, {
              title: params.row.customerName,
              event: 'onLinkUser',
              data: params.row
            })
          }
        },
        header: {
          filter: {
            title: '客户姓名',
            key: 'customerName',
            placeholder: '请输入客户姓名',
            input: {}
          }
        }
      },
      {
        title: '客户手机号',
        field: 'customerPhone',
        minWidth: 130,
        header: {
          filter: {
            title: '客户手机号',
            key: 'customerPhone',
            placeholder: '请输入至少4位客户手机号',
            input: {},
            check: (val) => {
              if (val.customerPhone.length < 4) {
                _this.$Message.warning('请输入至少4位客户手机号。')
              }
              return !(val.customerPhone.length < 4)
            }
          }
        }
      },
      {
        title: '退购房源',
        field: 'roomName',
        minWidth: 120
        // header: {
        //   filter: houseOpt.createRoom({
        //     key: 'roomId',
        //     title: '所属房源'
        //   })
        // }
      },
      {
        title: '面积（㎡）',
        field: 'area',
        minWidth: 150
      },
      {
        title: '认购单价（元）',
        field: 'price',
        minWidth: 150,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        title: '认购总价（元）',
        field: 'sumPrice',
        minWidth: 150,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        title: '定金金额（元）',
        field: 'deposit',
        minWidth: 150,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        title: '认购日期',
        field: 'subscriptionDate',
        minWidth: 180,
        header: {
          filter: dateOpt.range({
            title: '认购日期',
            startKey: 'subscriptionStartDate',
            endKey: 'subscriptionEndDate',
            disabled: (date) => {
              return date.valueOf() > new Date().getTime()
            },
            check: (val) => {
              if (_this.$tool.dateDiff(val.subscriptionStartDate, val.subscriptionEndDate).Y >= 2) {
                // 日期最大跨度两年
                _this.$Message.warning('认购日期跨度至多两年。')
              }
              return !(_this.$tool.dateDiff(val.subscriptionStartDate, val.subscriptionEndDate).Y >= 2)
            }
          })
        }
      },
      {
        title: '退购原因',
        field: 'backReason',
        minWidth: 180,
        cell: {
          text: {
            lineClamp: 2,
            webkitBoxOrient: 'vertical',
            display: '-webkit-box',
            whiteSpace: 'inherit'
          }
        }
      },
      {
        title: '退购日期',
        field: 'backDate',
        minWidth: 180,
        header: {
          filter: dateOpt.range({
            title: '退购日期',
            startKey: 'backSubStartDate',
            endKey: 'backSubEndDate',
            disabled: (date) => {
              return date.valueOf() > new Date().getTime()
            },
            check: (val) => {
              if (_this.$tool.dateDiff(val.backSubStartDate, val.backSubEndDate).Y >= 2) {
                // 日期最大跨度两年
                _this.$Message.warning('退购日期跨度至多两年。')
              }
              return !(_this.$tool.dateDiff(val.backSubStartDate, val.backSubEndDate).Y >= 2)
            }
          })
        }
      },
      {
        title: '对应认购单编号',
        field: 'orderCode',
        minWidth: 180,
        slots: {
          default: (params, h) => {
            return table.cell.btn(_this, h, {
              title: params.row.orderCode,
              event: 'detailFun',
              data: [params.row, 'RG']
            })
          }
        }
      },
      {
        title: '所属产品',
        field: 'productName',
        minWidth: 180,
        cell: {
          text: {
            lineClamp: 2,
            webkitBoxOrient: 'vertical',
            display: '-webkit-box',
            whiteSpace: 'inherit'
          }
        }
      },
      {
        title: '渠道名称',
        field: 'channelName',
        minWidth: 120
        // header: {
        //   filter: houseOpt.createChannel({
        //     key: 'channelId',
        //     title: '渠道名称'
        //   })
        // }
      },
      {
        title: '合作类型',
        field: 'channelType',
        minWidth: 120,
        header: {
          filter: channelTypeMgr.create({
            key: 'cooperationType'
          })
        },
        cell: {
          text: {
            type: 'dict',
            key: 'cooperationTypeFinal'
          }
        }
      },
      {
        field: 'channelCostValue',
        title: '渠道费用(元)',
        width: 130,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        field: 'channelFitName',
        title: '打法名称',
        width: 130,
        header: {
          filter: planOpt.createNoBuild({
            key: 'planIds',
            title: '打法名称'
          })
        }
        // header: {
        //   filter: planOpt.create({
        //     key: 'planId',
        //     title: '打法名称',
        //     data: {
        //       to: (info) => {
        //         let channelIdList = []
        //         info.forEach((ele) => {
        //           let channelId = ele.value.split('/')[1]
        //           channelIdList.push(channelId)
        //         })
        //         return {
        //           channelIdList: channelIdList.join(',').split(',')
        //         }
        //       }
        //     }
        //   })
        // }
      },
      {
        title: '认购人',
        field: 'contractorName',
        minWidth: 150
        // header: {
        //   filter: nameOpt.createContractor({
        //     key: 'contractorId',
        //     title: '认购人'
        //   })
        // }
      },
      {
        title: '审核人',
        visible: _this.curState == '0' ? false : true,
        field: 'auditName',
        minWidth: 120
      },
      {
        title: '操作',
        fixed: 'right',
        width: 140,
        align: 'center',
        slots: {
          default: (params, h) => {
            let btns = []
            btns.push({
              title: '查看',
              event: 'detailFun',
              data: [params.row, 'UR']
            })

            btns.push({
              title: params.row.businessResult == 3 && _this.unlocking('0206100101') ? '审核' : '',
              event: 'detailFun',
              data: [params.row, 'UR']
            })

            return table.cell.btns(_this, h, btns)
          }
        }
      }
    ]
  }
}

export default Column
